﻿using System.Collections.Generic;
using System.Linq;
using Apps.Web.Core;
using Apps.IBLL.S;
using Apps.Locale;
using System.Web.Mvc;
using Apps.Common;
using Apps.IBLL;
using Apps.Models.S;
using Microsoft.Practices.Unity;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Data;
using Apps.Models;

namespace Apps.Web.Areas.S.Controllers
{
    public class CollectSampleController : BaseController
    {
        [Dependency]
        public IS_COLLECTSAMPLEBLL m_BLL { get; set; }
        ValidationErrors errors = new ValidationErrors();

        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetList(GridPager pager, string batch,string startdate,string enddate)
        {
            var list = m_BLL.GetList_RC(ref pager, batch, startdate, enddate);
            GridRows<V_S_COLLECTSAMPLE_RC> grs = new GridRows<V_S_COLLECTSAMPLE_RC>();
            grs.rows = list;
            grs.total = pager.totalRows;
            return Json(grs);
        }


        [HttpPost]
        [SupportFilter(ActionName = "Export")]
        public JsonResult CheckExportData(string batch, string startdate, string enddate)
        {
            List<V_S_COLLECTSAMPLE_RC> list = m_BLL.GetList_RC(ref GridPager.NotPage,batch, startdate, enddate);
            if (list.Count().Equals(0))
            {
                return Json(JsonHandler.CreateMessage(0, "没有可以导出的数据"));
            }
            else
            {
                return Json(JsonHandler.CreateMessage(1, "可以导出"));
            }
        }
        [SupportFilter]
        public ActionResult Export(string batch, string startdate, string enddate)
        {
            List<V_S_COLLECTSAMPLE_RC> list = m_BLL.GetList_RC(ref GridPager.NotPage, batch, startdate, enddate);
            JArray jObjects = new JArray();
            foreach (var item in list)
            {
                var jo = new JObject();
                jo.Add("批次编码", item.BATCH_CODE);
                jo.Add("采样号", item.SAMPLE_CODE);
                jo.Add("物料", item.MATERIEL_NAME);
                jo.Add("矿点", item.MINE_NAME);
                jo.Add("供应商", item.SUPPLIER_NAME);
                jo.Add("运输商", item.TRANSPORT_NAME);
                jo.Add("样桶RFID", item.RFID);
                jo.Add("分样号", item.COLLECT_CODE);
                if (item.SAMPLE_TYPE == 1)
                {
                    jo.Add("样品类型", "入炉");
                }
                else
                {
                    jo.Add("样品类型", "入厂");
                }
                jo.Add("重量", item.WEIGHT);
                if (item.WEIGHT_STATUS == 1)
                {
                    jo.Add("称重稳定", "不稳定");
                }
                else
                {
                    jo.Add("称重稳定", "稳定");
                }
                jo.Add("收样人", item.R_USER_NAME);
                //jo.Add("收样时间", item.RECEIVE_TIME);//20180829吴兵要求去掉
                jo.Add("送样人", item.S_USER_NAME);
                jo.Add("收样时间", item.RECEIVE_TIME);
                jo.Add("是否已合样", (item.BLENDED.Value? "合样样品": "原始样"));
                jo.Add("是否已制样", (item.MADE.Value ? "已制样" : "未制样"));
                jObjects.Add(jo);
            }
            var dt = JsonConvert.DeserializeObject<DataTable>(jObjects.ToString());
            var exportFileName = string.Concat(
                "制样收样信息",
                DateTime.Now.ToString("yyyyMMdd"),
                ".xlsx");
            return new ExportExcelResult
            {
                SheetName = "Sheet1",
                FileName = exportFileName,
                ExportData = dt
            };
        }
    }
}
